﻿using System;
using System.Web.Mvc;
using Sharp.DataTransferObject.CMS;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts.CMS;
using Sharp.WebBooter.Areas.CMS.Models;

namespace Sharp.WebBooter.Areas.CMS.Controllers
{
    public class GatherController : Controller
    {
        // GET: Control/Gather
        public ActionResult Index()
        {
            return View();
        }

        [Permission(Name = "新增", Order = 1)]
        public ActionResult Create()
        {
            GatherViewModel gatherViewModel = new GatherViewModel();
            return View("Edit", gatherViewModel);
        }



        [Permission(Name = "编辑", Order = 2)]
        public ActionResult Edit(Guid id)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var model = proxy.Channel.GetGatherInfo(id).Data.To<GatherViewModel>();

                return View("Edit", model);
            }
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Save(GatherDTO dto)
        {
            if (dto == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }


            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.SaveGather(dto);
                res.RedirectToUrl = Url.Action("Index");

                return this.JsonNet(res);
            }
        }
        [HttpPost]
        public ActionResult GetPaged(DataTableParameter param)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var page = proxy.Channel.GetGatherPaged(param.ToPagingFeature());

                return this.Store(page);
            }
        }

        [HttpPost]
        [Permission(Name = "删除", Order = 3)]
        public ActionResult Delete(Guid[] ids)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var res = proxy.Channel.DeleteGathers(ids);
                return this.JsonNet(res);
            }
        }

        public ActionResult WrokConfig(string ids)
        {
            ViewBag.Ids = ids;
            return View();
        }
        [HttpPost]
        public ActionResult GatherClassPage(CategoryParameterDTO cp)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var cgr = proxy.Channel.GatherCategoryPage(cp);

                return this.Json(cgr);
            }
        }
        public ActionResult GatherWeb(WebParameterDTO wp)
        {
            using (var proxy = new ServiceProxy<ICMSService>())
            {
                var cpgr = proxy.Channel.GatherPage(wp);

                return this.Json(cpgr);
            }
        }

        public ActionResult Node(Guid? id, Guid? parentId)
        {
            var vm = new GatherViewModel
            {

                ParentId = parentId
            };
            if (id.HasValue)
            {
                vm.Id = id.Value;
            }
            if (id.HasValue)
            {
                using (var proxy = new ServiceProxy<ICMSService>())
                {
                    vm = proxy.Channel.GetGatherInfo(id.Value).Data.To<GatherViewModel>();
                }
            }
            return View(vm);
        }
    }
}